package com.gcorp.idealgas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;



public class MainDialog extends JFrame
{
	
	private Simulation sim;
	
	private float RelativeX;
	
	private float RelativeY;
	
    MainDialog()
    {
        initUI();
        initSimulation();
        initProgramStyle();
    }
    private void initUI()
    {
    	
    	initMenubar();
    	initGeneralSettings();

        
    }
    private void initGeneralSettings()
    {
       
    	//setSize(510, 555);
        setSize(800,800);
        setTitle("Gas Simulation by George Misuradze");
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void initProgramStyle()
    {
    	try 
    	{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} 
    	catch (ClassNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	catch (InstantiationException e) 
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	catch (IllegalAccessException e) 
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    	catch (UnsupportedLookAndFeelException e) 
    	{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	SwingUtilities.updateComponentTreeUI(MainDialog.this);
    }
    private void initMenubar()
    {
    	JMenuBar menubar = new JMenuBar();
    	
    	JMenu simulationControl = new JMenu("Simulation");
    	simulationControl.setMnemonic(KeyEvent.VK_S);
    	
    	JMenuItem startSimulation = new JMenuItem("Start/Continue");
    	startSimulation.setToolTipText("Starts the simulation with current settings.");
    	startSimulation.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				sim.start();
			}
    		
    	});
    	
    	
    	JMenuItem stopSimulation = new JMenuItem("Stop");
    	stopSimulation.setToolTipText("Stops the simulation.");
    	stopSimulation.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				sim.stop();
				
				
			}
    		
    	});
    	
    	
    	JMenuItem restartSimulation = new JMenuItem("Restart");
    	restartSimulation.setToolTipText("Restarts the simulation.");
    	restartSimulation.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				sim.restart();
			}
    		
    	});
    	
    	JMenuItem exit = new JMenuItem("Exit");
    	exit.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				System.exit(0);
			}
    		
    	});
    	
    	simulationControl.add(startSimulation);
    	simulationControl.add(stopSimulation);
    	simulationControl.add(restartSimulation);
    	simulationControl.add(exit);
    	
    	
    	
    	JMenu managerControl = new JMenu("Manager");
    	
    	JMenuItem addMolecule = new JMenuItem("Add Molecule");
    	addMolecule.setToolTipText("Adds new molecule on the scene.");
    	addMolecule.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(final ActionEvent e) 
			{
				
			
				AddNewMoleculeDialog d = AddNewMoleculeDialog.getInstance();
				d.reset();
				d.setVisible(true);
				

				
				
				
				
			}
    		
    	});
    	
    	JMenuItem addBaffle = new JMenuItem("Add Baffle");
    	addBaffle.setToolTipText("Adds new baffle to the scene.");
    	addBaffle.addActionListener(new ActionListener()
    	{

			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				AddNewBaffleDialog d = new AddNewBaffleDialog();
				
				d.setVisible(true);
			}
    		
    	});
    	
    	managerControl.add(addMolecule);
    	managerControl.add(addBaffle);
    	
    	
    	menubar.add(simulationControl);
    	menubar.add(managerControl);
    	
    	setJMenuBar(menubar);
    	
    }
    private void initSimulation()
    {
    	sim = Simulation.getInstance();
        sim.setSize(500, 500);
        add(sim);
    }
	public static void main(String[] args)
	{
	    SwingUtilities.invokeLater(new Runnable()
	    {

            @Override
            public void run()
            {
                MainDialog m = new MainDialog();
                m.setVisible(true);
                
                
                
            }
	        
	    });
	}

}